package com.party.admin.biz.subject;

import com.party.admin.biz.article.ArticleBizService;
import com.party.admin.biz.channel.ChannelDeleteBizService;
import com.party.admin.biz.file.FileBizService;
import com.party.common.utils.StringUtils;
import com.party.core.model.article.ArticleGroup;
import com.party.core.model.channel.Channel;
import com.party.core.model.subject.Subject;
import com.party.core.model.subject.SubjectApply;
import com.party.core.service.article.IArticleGroupService;
import com.party.core.service.channel.IChannelService;
import com.party.core.service.cos.CosBizService;
import com.party.core.service.subject.ISubjectApplyService;
import com.party.core.service.subject.ISubjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * 专题删除业务
 *
 * @author yifeng
 * @date 2018/7/16 0016
 * @time 下午 14:10
 */
@Service
public class SubjectDeleteBizService {
    @Autowired
    private CosBizService cosBizService;
    @Autowired
    private FileBizService fileBizService;
    @Autowired
    private ISubjectService subjectService;
    @Autowired
    private ISubjectApplyService subjectApplyService;
    @Autowired
    private ArticleBizService articleBizService;
    @Autowired
    private IChannelService channelService;
    @Autowired
    private ChannelDeleteBizService channelDeleteBizService;
    @Autowired
    private IArticleGroupService articleGroupService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 删除专题
     *
     * @param subjectId 专题编号
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean deleteSubjectBiz(String subjectId) {
        boolean endResult = true;
        String memberId = null;
        try {
            if (StringUtils.isEmpty(subjectId)) {
                endResult = false;
                return endResult;
            }
            Subject subject = subjectService.get(subjectId);
            if (subject == null) {
                endResult = false;
                return endResult;
            }
            memberId = subject.getMemberId();
            List<SubjectApply> subjectApplies = subjectApplyService.getBySubjectId(subjectId);
            for (SubjectApply subjectApply : subjectApplies) {
                boolean result = this.deleteSubjectApplyBiz(subjectApply.getId());
                if (!result) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return false;
                }
            }
            subjectService.delete(subjectId);
        } catch (Exception e) {
            endResult = false;
            logger.error("删除专题失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } finally {
            if (endResult) {
                try {
                    String filePath = fileBizService.getUploadCIPath(memberId, "subject");
                    String detailPath = CosBizService.getDeleteQrCodePath(filePath, subjectId);
                    cosBizService.deleteFile(detailPath);
                } catch (Exception e) {
                    logger.error("删除专题二维码失败", e);
                }
            }
        }
        return endResult;
    }


    /**
     * 删除专题栏目
     *
     * @param subjectApplyId 栏目编号
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean deleteSubjectApplyBiz(String subjectApplyId) {
        boolean endResult = true;
        try {
            if (StringUtils.isEmpty(subjectApplyId)) {
                endResult = false;
                return endResult;
            }
            SubjectApply subjectApply = subjectApplyService.get(subjectApplyId);
            if (subjectApply == null) {
                endResult = false;
                return endResult;
            }
            if (null != subjectApply.getType()) {
                if (SubjectApply.TYPE_ARTICLE.equals(subjectApply.getType())) {
                    if (StringUtils.isNotEmpty(subjectApply.getTargetId())) {
                        // 文章
                        endResult = articleBizService.deleteArticleBiz(subjectApply.getTargetId());
                    }
                } else if (SubjectApply.TYPE_ARTICLE_GROUP.equals(subjectApply.getType())) {
                    // 文章组
                    Channel channel = channelService.get(subjectApply.getTargetId());
                    if (channel != null) {
                        endResult = channelDeleteBizService.deleteChannelBiz(channel.getId());
                    }
                    ArticleGroup articleGroup = articleGroupService.get(subjectApply.getTargetId());
                    if (articleGroup != null) {
                        endResult = articleBizService.deleteArticleGroupBiz(articleGroup.getId());
                    }
                }
            }

            subjectApplyService.delete(subjectApplyId);
        } catch (Exception e) {
            endResult = false;
            logger.error("删除专题栏目失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return endResult;
    }
}
